﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESHOP.Domain;
using System.Data.Common;
namespace ESHOP.DAO
{
   public class GioHangDAO:BaseDAO
    {
       
       public GIOHANG Get(int ma)
       {
           return _db.GIOHANGs.Where(p => p.ID==ma&&p.ISSHOW==true).SingleOrDefault();
       }
       public List<GIOHANG> GetList()
       {
           return _db.GIOHANGs.ToList();
       }
       public List<GIOHANG> GetListBuyCompanyId(string companyId)
       {
           return _db.GIOHANGs.Where(p => p.MACT == companyId && p.ISSHOW==true).ToList(); ;
       }
       public int Update(GIOHANG objui)
       {

           try
           {
               var objdb = Get(objui.ID);
               if (objdb != null)
               {
                   objdb.MACT = objui.MACT;
                   objdb.GIA = objui.GIA;                
                   _db.SubmitChanges();
               }
           }
           catch
           {
               return 0;
           }
           return 1;
       }

       public int Insert(GIOHANG objui)
       {
           DbTransaction trans = null;
           try
           {
               _db.Connection.Open();
               trans = _db.Connection.BeginTransaction();
               _db.Transaction = trans;
               _db.GIOHANGs.InsertOnSubmit(objui);
               _db.SubmitChanges();
               trans.Commit();
           }
           catch
           {
               return 0;
           }
           return 1;
       }

       public int Delete(int ma)
       {
           try
           {
               var obj = Get(ma);
               if (obj != null)
               {

                   obj.ISSHOW = false;
                   _db.SubmitChanges();
               }

           }
           catch
           {
               return 0;
           }
           return 1;
       }

       public int UpdateNumber(int id,int soluong)
       {
           var objbd = Get(id);
           if (objbd != null)
           {
               objbd.SOLUONG = objbd.SOLUONG + soluong;
               objbd.GIA = objbd.SANPHAM.GIA * objbd.SOLUONG;
               _db.SubmitChanges();
               return 1;
           }
           else
           {
               return 0;
           }
       }
    }
}
